package com.automobile.backstage.dao;

import com.automobile.backstage.domain.Smart;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-03-25 17:37
 */
public interface SmartDao {

    @Select("select * from smart where id != 0 ORDER BY id DESC")
    @Results(id = "smartAll",value = {
            @Result(property = "id",column = "id",id = true),
            @Result(property = "title",column = "title"),
            @Result(property = "message",column = "message"),
            @Result(property = "date",column = "date"),
            @Result(property = "userId",column = "userId"),
            @Result(property = "length",column = "length"),
            @Result(property = "width",column = "width"),
            @Result(property = "height",column = "height"),
            @Result(property = "num",column = "num"),
            @Result(property = "driving",column = "driving"),
            @Result(property = "motor",column = "motor"),
            @Result(property = "cell",column = "cell"),
            @Result(property = "capacity",column = "capacity"),
            @Result(property = "time1",column = "time1"),
            @Result(property = "time2",column = "time2"),
            @Result(property = "maxSpeed",column = "maxSpeed"),
            @Result(property = "imgs",column = "id",many = @Many(select = "com.automobile.backstage.dao.ImgDao.findImgs2",fetchType = FetchType.LAZY)),
            @Result(property = "user",column = "userId",one = @One(select = "com.automobile.backstage.dao.UserDao.findBySuggest",fetchType = FetchType.LAZY))
    })
    List<Smart> findAll();

    @Select("select * from smart where id = #{id}")
    @ResultMap("smartAll")
    Smart findById(int id);

    @Delete("delete from smart where id = #{id}")
    void delete(int id);

    @Insert("insert into smart(title,message,money,userId,length,width,height,num,driving,motor,cell,capacity,time1,time2,maxSpeed,status) " +
            "values(#{title},#{message},#{money},#{userId},#{length},#{width},#{height},#{num},#{driving},#{motor},#{cell},#{capacity},#{time1},#{time2},#{maxSpeed},#{status})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    void save(Smart smart);

    @Update("update smart set title = #{title},message = #{message},money = #{money} ,userId = #{userId},length = #{length},width = #{width},height = #{height},num = #{num},driving = #{driving},motor = #{motor},cell = #{cell},capacity = #{capacity},time1 = #{time1},time2 = #{time2},maxSpeed = #{maxSpeed},status = #{status} where id = #{id}")
    void update(Smart smart);

    @Update("update smart set status = #{status} where id = #{id}")
    void updateStatus(@Param("status") int status,@Param("id") int id);
}
